const tool = {
  // 判断是否颜色
  isColor(str) {
    return /^#([A-Fa-f0-9]{6})$/.test(str);
  },
  hexToRgb(hex) {
    if (!tool.isColor(hex)) {
      console.error("颜色格式错误", hex);
      return "";
    }
    // 提取颜色的RGB值
    const match = hex.match(/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i);
    const r = parseInt(match[1], 16);
    const g = parseInt(match[2], 16);
    const b = parseInt(match[3], 16);
    return [r, g, b];
  },
  hexToRgba(hex, alpha) {
    // 提取颜色的RGB值
    const rgb = tool.hexToRgb(hex);
    if (!rgb) {
      return "";
    }
    return `rgba(${rgb[0]},${rgb[1]},${rgb[2]},${alpha})`;
  },
};

export default tool;
